package com.laikan.reader.core.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import java.io.InputStream;

/**
 * 不要删掉
 * Created by wx on 16/8/30.
 */
public class BimapUtil {



    private Bitmap setImage(Bitmap bitmap,int screeHeight,int screenWidth) {
        //获取屏幕的宽高

        int fixHeight = (int)(screeHeight * (354f / 1136f));
        int newHeight = bitmap.getWidth() * fixHeight / screenWidth;
        return scaleImage(Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), newHeight), screenWidth, fixHeight);

    }

    /**
     * 缩放图片       * @param bm 要缩放图片      * @param newWidth 宽度      * @param newHeight 高度      * @return处理后的图片
     */
    public static Bitmap scaleImage(Bitmap bm, int newWidth, int newHeight) {
        if (bm == null) {
            return null;
        }
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float)newWidth) / width;
        float scaleHeight = ((float)newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        if (bm != null & !bm.isRecycled()) {
            bm.recycle();//销毁原图片
            bm = null;
        }
        return newbm;
    }


    private Bitmap scaleBig(int heightAdd,Bitmap matrixPull) {
        if (matrixPull == null) {
            return null;
        }
        int width = matrixPull.getWidth();
        int height = matrixPull.getHeight();
        double scale = heightAdd / height;
        float scaleWidth = (float)(width * scale);
        float scaleHeight = (float)(height * scale);
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizeBmp = Bitmap.createBitmap(matrixPull, 0, 0, width, height, matrix, true);
        return resizeBmp;
    }

    /**
     * 通过资源id转化成Bitmap      * @param context      * @param resId      * @return
     */
    public Bitmap readBitmapById(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

}
